package com.zzj.tch;

import org.junit.Test;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * 加解密
 * @author: zzj
 * @date: 2017/12/25
 * Time: 9:17
 *
 *
 * {"code":"1","message":"D5B44BA84A2A6CCE6F661521185B58AF0BEB544092F929BFC6FA18C1794B0C089A8CC7E2632079625C16BF029A41929B22D11A1C8A882BB4F16D2CBB435423A0BB9585D55E35963AB69E56AE3B436956C1CB823DB107E92CAC4B47BF6750DCE9090DE85C08D311BA620D2DB0C2E6FE2FD1F01E4CD6E70FB14273F8A8571211D106F2F131C60AB56F2B17D6BA03BA3D73ADBE946D32763D8D2307F60675A300B85548004B9903988DAFA66FF62158881088FBF2892924C44C9D004D0ABE76272083D9C20E6B5A504939DB803690D2ABB6EA76BEF430D9BCE5924B272291DBA4B72DE2D39E066DEEDE51194A1199151D378D420E1772AD73495AFB17048BB78B4F60D601396EE521ECC0AB02A369F3767AEDB417135D88F34A90023DE7F5E8F4350C4CA41AB767BBAD38B675C1A147E337EBF8805C166A900A37606F7C81AEC18CF77DDA36A62640BBDC779C6B26B2F56B3AA929E3B82A361BA57388731DEF9C93CA228254E33C836F3E1FCBAEE7A9E05A58D57C3FD1196C057B1F5E7644321AE775319DB3EC369611A08C595F489C71A828AF05BDDEE66C9D7B487D94955D8C725B752FBBA8FDDDDBECC8F3B15790D64BC61C885F27D968111DA259403FE8B4929593BC4611807EE7B45A19DB11044A8348766035F401602E26C34C4A1091E73EB0EAF3B3D039C1B3...91030AAB570DE4FC18E189AC9A227BECFF8A3228F059AD38A881FB5B75FC0E4CDE04FB0E801042C91F91F09B65D5B72D5DBB7ED807CC4960BAE3E42992AC36901C14C0868532E75806F1F36F92968A4F28AC6445842ADA0DF781FACC2C6ECC3208D7D8EF794487E50C86C7413FAB012DCC2E3B2242C7FB8581184DE3BC862CA196929E6990394C36E8A38D927CB49363D037EFBC209F0C2159188433A423DFD4A8A191DB8923091FCDA499D93F988A9104D345A289479939626752DC43082512CFD1B5665F82D74FE8D5045E906AC5A55AE4BD90750707C4989568DD62714C1BED54E067A126EC23F345628E722E6BA78E95D418A614A8A38EB5312B47316697276C8483E4A125E24A46A4702772363BD8DFE5ECB1F42B8CE6A2FC989D2309D93A1EE2DB16A39AEC60C84E04D479F766618726E0912E555C3DF999F42BCC7BE11BD1A79DD8BC126871591BD741E30F6B023A051F4BB933FFAF37E80D09585BC04FD0B51B87E6E8DEE359CA4C8CDB4F913D0B8E053D35CD55BC91112FFECC1B09D59B7FC17F641289C1E380CEB1300CC9509E99023BF8122350E4FAFF2A02328A086C157D27304CC3DBF4A058C431F89616C51B0D280EC994C224D27479718C77DD42E8761963940E1E40DC076E135891ECC2D1FDE1B0814D10057089D94254032B41012045275B78F31EC67AE0C513A137F5"}
 *
 *
 *
 */
public class TcOcxDecode {
    private String strDefaultKey = "I71C1G6AJnCHLFi4sbI5dEr6SL0nBwbQCHJJQx9gpXc39iIqsVfNNwYhE4PR8EAzeBcwsZe4PEVpYV5D5OrUCugPBhdzgHqrJYTYrgQL9JWUwjWKEYqouafEVN7jNWOx";

    private Cipher decryptCipher = null;
    private Cipher encryptCipher = null;
    /**
     *  解密
     */

    public String getDesString(String strMi) throws Exception
    {
        return new String(decrypt(hexStr2ByteArr(strMi)), "GBK");
    }

    private byte[] decrypt(byte[] arrB) throws Exception
    {
        Key key = getKey(strDefaultKey.getBytes());
        /**
         * 加密
         */
        this.encryptCipher = Cipher.getInstance("DES");
        this.encryptCipher.init(1, key);
        //解密
        this.decryptCipher = Cipher.getInstance("DES");
        this.decryptCipher.init(2, key);

        return this.decryptCipher.doFinal(arrB);
    }

    private Key getKey(byte[] arrBTmp) throws Exception
    {
        byte[] arrB = new byte[8];
        for (int i = 0; (i < arrBTmp.length) && (i < arrB.length); i++) {
            arrB[i] = arrBTmp[i];
        }
        Key key = new SecretKeySpec(arrB, "DES");

        return key;
    }

    private static byte[] hexStr2ByteArr(String strIn) throws Exception
    {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;

        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i += 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[(i / 2)] = (byte)Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    @Test
    public void test1() throws Exception {
        String s = this.getDesString("9AD88A3CE5FFD51C486AEA6035E42ECA109510C85936590CD511764752CDF82A61518F81725A607B6C82D5E7896FF16AC7390DF44877167E");
        System.out.println(s);
    }

}
